//获取小说接口
'use strict';
let uniId = require('uni-id')
let db = uniCloud.database()
let dbCommad = db.command
exports.main = async (event, context) => {
	//处理url化的post接口,如果你的接口无需暴露接口给别人用就无需关心下面的代码
	if(event.body){
		let ip =event.headers['x-real-ip']
		event={
			...JSON.parse(event.body)
		},
		//这里需要手动传入context参数
		uniId = uniId.createInstance({ // 创建uni-id实例，其上方法同uniID
		    context:{
				PLATFORM:'app-plus',
				APPID:'__UNI__B10F710',
				LOCALE:'zh-Hans',
				CLIENTIP:ip
			}, // 传入context防止不同请求互相影响
		})
	}
	//end 处理url化
	
	//校验token
	let result = await payloadToken(event)
	if (result.code != 0) { //校验不成功【如果校验成功返回uid】
		return result
	}

	let res = {}
	//添加轮播图
	if (event.active == 'addBanner') {
		res = await addBanner(event)
	}
	//修改轮播图
	if (event.active == 'editBanner') {
		res = await editBanner(event)
	}
	//获取轮播图列表
	if(event.active=='getBannerList'){
		res = await getBannerList(event)
	}
	//删除轮播图
	if(event.active=='delectBanner'){
		res = await delectBanner(event)
	}
	// 返回数据给客户端
	return res
};
//修改轮播图
async function editBanner(event){
	let result = {}
	if(event.params&&event.params.fileId&&event.params._id&&event.params.bookId){
		//先删除原先保存在云存储中的图片
		let banner= await db.collection('bannerList').doc(event.params._id).get()
		if(banner.affectedDocs>0&&(event.params.fileId!==banner.data[0].fileId)||(event.params.bookId!==banner.data[0].bookId)||(event.params.slot!==banner.data[0].slot)){
			//只有改变了轮播图才删除之前的图片
			if(event.params.fileId!==banner.data[0].fileId){
				let fileList=[banner.data[0].fileId]
				//删除云存储中的图片
				let del = await uniCloud.deleteFile({fileList});
				//删除fileList数据库中的数据
				let fil = await db.collection('fileList').where({fileId:banner.data[0].fileId}).remove()
			}
			//然后修改
			let data = await db.collection('bannerList').doc(event.params._id).update({
				bookName:event.params.bookName,
				bookId:event.params.bookId,
				slot:event.params.slot,
				fileId:event.params.fileId,
			})
			result = {
				code: 0,
				data,
				msg: '成功'
			}
		}else{
			result = {
				code: 400,
				msg: '未改变数据'
			}
		}
	}else {
		result = {
			code: 400,
			msg: '参数错误'
		}
	}
	return result
}
//删除轮播图
async function delectBanner(event){
	let result = {}
	if(event.params&&event.params.fileId&&event.params.bannerId){
		//先删除轮播图
		let data=await db.collection('bannerList').doc(event.params.bannerId).remove()
		//再删除轮播图图片
		let data2=await db.collection('fileList').where({fileId:event.params.fileId}).remove()
		//最后删除云存储的轮播原图
		let del = await uniCloud.deleteFile({ fileList:[event.params.fileId]});
		result = {
			code: 0,
			data,
			msg: '成功'
		}
	}else {
		result = {
			code: 400,
			msg: '参数错误'
		}
	}
	return result
}
//获取轮播图列表
async function getBannerList(event){
	let data=await db.collection('bannerList').orderBy('slot','asc').get()
	let result = {
		code: 0,
		data:data.data,
		msg: '成功'
	}
	return result
}
//添加轮播图
async function addBanner(event){
	let result = {}
	if(event.params&&event.params.fileId){
		let data=await db.collection('bannerList').add({
			fileId:event.params.fileId,
			bookName:event.params.bookName,
			bookId:event.params.bookId,
			slot:event.params.slot,
		})
		result = {
			code: 0,
			data,
			msg: '成功'
		}
	}else {
		result = {
			code: 400,
			msg: '参数错误'
		}
	}
	return result
}
//校验token
async function payloadToken(event) {
	//校验token
	let payload = {}
	//用户带的参数
	let params = event.params || {}
	if (!event.token) {
		return {
			code: 403,
			msg: '用户未登录或登录失效'
		}
	}
	//校验token
	payload = await uniId.checkToken(event.token);
	//校验失败
	if (payload.code && payload.code > 0) {
		return payload
	}
	params.uid = payload.uid //校验成功之后会返回用户Id
	return {
		code: 0,
		...params
	}
}
